<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.deptAddOrUpdate.edit('','add')">新建</a-button>
    </div>
    <a-table
      :columns="columns"
      :dataSource="tableData"
      :loading="loading"
      :rowKey="record => record.deptId"
      :pagination="false"
    >
    </a-table>
    <dept-add-or-update
      ref="deptAddOrUpdate"
      @ok="handleOk"/>
  </a-card>
</template>

<script>
import { fetchList, delObj } from '@api/sys/dept'
import DeptAddOrUpdate from './deptAddOrUpdate'
export default {
  name: 'Menu',
  components: {
    DeptAddOrUpdate
  },
  data () {
    return {
      loading: false,
      columns: [
        {
          title: '部门名称',
          dataIndex: 'name'
        },
        {
          title: 'id',
          dataIndex: 'deptId'
        },
        {
          title: '排序号',
          dataIndex: 'orderNum'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          width: '25%',
          customRender: (record) => {
            return (
              <div>
                <a onClick={(e) => this.edit(record)}
                >修改</a>
                <a-divider type="vertical" />
                <a-popconfirm title="是否删除部门" onConfirm={() => this.delete(record)} okText="是" cancelText="否">
                  <a-icon slot="icon" type="question-circle-o" style="color: red" />
                  <a href="javascript:;" style="color: red">删除</a>
                </a-popconfirm>
                <a-divider type="vertical" />
                <a href="javascript:;"
                  onClick={(e) => this.show(record)}
                >查看</a>
              </div>
            )
          }
        }
      ],
      tableData: []
    }
  },
  methods: {
    init () {
      this.loading = true
      fetchList().then(res => {
        this.tableData = res.data
        this.loading = false
      })
    },
    edit (record) {
      this.$refs.deptAddOrUpdate.edit(record.deptId, 'edit')
    },
    delete (record) {
      delObj(record.deptId).then(_ => {
        this.$message.info('删除成功')
        this.init()
      })
    },
    handleOk () {
      this.init()
    },
    show (record) {
      this.$refs.deptAddOrUpdate.edit(record.deptId, 'show')
    }
  },
  created () {
    this.init()
  }
}
</script>

<style scoped>

</style>
